<template>
	<view class="content">
		<u-form :model="userInfo" ref="form1" class="user-list list-border">
			<u-form-item label="儿童姓名" prop="name" label-width="140">
				<span style="color: red;">*</span>
				<u-input placeholder="请填写儿童姓名" v-model="userInfo.name" />
			</u-form-item>
			<u-form-item label="性别" prop="sex" label-width="140">
				<u-input v-model="userInfo.sex" placeholder="请选择性别" type="select" @click="select1.show=true" />
			</u-form-item>
			<u-form-item label="身份证" prop="sfid" label-width="140">
				<span style="color: red;">*</span>
				<u-input placeholder="请填写身份证" type="idcard" :maxlength="18" v-model="userInfo.sfid" />
			</u-form-item>
			<u-form-item label="手机号" prop="phone" label-width="140">
				<span style="color: red;">*</span>
				<u-input placeholder="请填写手机号" :maxlength="11" v-model="userInfo.phone" />
			</u-form-item>
			<u-form-item label="家长身份证" prop="jzsfid" label-width="160">
				<u-input placeholder="请填写家长身份证" type="idcard" :maxlength="18" v-model="userInfo.jzsfid" />
			</u-form-item>
			<u-form-item label="家长姓名" prop="jzname" label-width="140">
				<span style="color: red;">*</span>
				<u-input placeholder="请填写家长姓名" :maxlength="11" v-model="userInfo.jzname" />
			</u-form-item>
			<u-form-item label="幼儿园" prop="yeyuan" label-width="140">
				<span style="color: red;">*</span>
				<u-input v-model="userInfo.yeyuan" placeholder="选择幼儿园" type="select" @click="select2.show=true" />
			</u-form-item>
			<u-form-item label="年级" prop="sex" label-width="140">
				<u-input v-model="userInfo.TJGradeName" placeholder="选择年级" type="select" @click="select3.show=true" />
			</u-form-item>

			<u-form-item label="班级" prop="age" label-width="140">
				<u-input placeholder="请填写班级" :maxlength="11" v-model="userInfo.TJClassName" />
			</u-form-item>
			<u-form-item label="地区" label-width="140" @click="regionshow=true">
				<view class="">
					<view class="dis" v-if="adderssinfor.array!=''">
						<view class="uni-input" v-for="(item,index) in adderssinfor.array" :key="index">{{item}}&emsp;
						</view>
					</view>
					<view v-else style="color: #b5b5b5;">
						<span>请选择省市县</span>
					</view>
				</view>
			</u-form-item>
			<u-form-item label="地址" prop="TJAddress" label-width="140">
				<span style="color: red;">*</span>
				<u-input placeholder="请填家庭地址" :maxlength="11" v-model="userInfo.TJAddress" />
			</u-form-item>
			<u-form-item label="既往病史" prop="TJIllness" label-width="140">
				<span style="color: red;">*</span>
				<u-input v-model="userInfo.TJIllness" placeholder="选择既往病史" type="select" @click="select4.show=true" />
			</u-form-item>
			<u-form-item label="其它既往病史" prop="lh" label-width="180">
				<u-input placeholder="其它既往病史" :maxlength="11" v-model="userInfo.TJOtherIll" />
			</u-form-item>
			<u-form-item label="过敏史" prop="lh" label-width="140">
				<u-input placeholder="过敏史" :maxlength="11" v-model="userInfo.TJAllergies" />
			</u-form-item>
		</u-form>
		<u-picker v-model="timeshow" mode="time" @confirm="timePickerChange" :default-time="timeinfor.time"></u-picker>
		<u-picker v-model="regionshow" mode="region" @confirm="bindPickerChange" :default-region="array"></u-picker>
		<u-select v-model="select1.show" :list="select1.list" @confirm="onSelect1"></u-select>
		<u-select v-model="select2.show" :titlea="true" :list="select2.list" @confirm="onSelect2"></u-select>
		<u-select v-model="select3.show" :list="select3.list" @confirm="onSelect3"></u-select>
		<u-select v-model="select4.show" :list="select4.list" @confirm="onSelect4"></u-select>
		<view class="bottom-button">
			<!-- <u-button shape="circle" type="primary" size="default" @click="submit">提交</u-button> -->
			<view class="button bacnone2" @click="submit">
				<span>提交</span>
			</view>
		</view>
	</view>
</template>

<script>
	import {
		mapState,
		mapGetters
	} from 'vuex'

	export default {
		data() {
			return {
				userInfo: {
					name: '',
					sex: '男',
					sfid: '',
					phone: '',
					jzsfid: '',
					jzname: '',
					yeyuan: '',
					yeyuansy: '',
					TJGradeCode: '',
					TJGradeName: '',
					TJClassName: '',
					checkbox: '',
					TJAddress: '',
					TJOtherIll: '',
					TJIllness: '',
					dyh: '',
					fjh: '',
					TJAllergies: '',
					TJChildId:''
				},
				array: ["内蒙古自治区", "乌兰察布市", "集宁区"],
				adderss: '',
				regionshow: false,
				timeshow: false,
				sexs: '请选择',
				show: false,
				timeinfor: {
					time: '',
				},
				adderssinfor: {
					AddressId: '',
					array: ["内蒙古自治区", "乌兰察布市", "集宁区"],
					Province: '内蒙古自治区',
					City: '乌兰察布市',
					County: '集宁区',
					name: '',
					phone: '',
					detial: '',
					checked: true
				},
				tips: '',
				value: '',

				select1: {
					show: false,
					current: {},
					list: [{
							value: 1,
							label: '男'
						},
						{
							value: 2,
							label: '女'
						}
					],
				},
				select3: {
					show: false,
					current: {},
					list: [{
							value: '托班',
							label: '托班'
						},
						{
							value: '小班',
							label: '小班'
						},
						{
							value: '中班',
							label: '中班'
						},
						{
							value: '大班',
							label: '大班'
						},
						{
							value: '大大班',
							label: '大大班'
						}
					],
				},
				select2: {
					show: false,
					current: {},
					list: [],
				},
				select4: {
					show: false,
					current: {},
					list: [],
				},
				useyzm: '',
				rules: {
					name: [{
						required: true,
						message: '请输入儿童姓名',
						// 可以单个或者同时写两个触发验证方式
						trigger: 'blur,change'
					}],
					jzname: [{
						required: true,
						message: '请输家长姓名',
						// 可以单个或者同时写两个触发验证方式
						trigger: 'blur,change'
					}],
					yeyuan: [{
						required: true,
						message: '请选择幼儿园',
						// 可以单个或者同时写两个触发验证方式
						trigger: 'blur,change'
					}],
					TJAddress: [{
						required: true,
						message: '请填写家庭地址',
						// 可以单个或者同时写两个触发验证方式
						trigger: 'blur,change'
					}],
					TJIllness: [{
						required: true,
						message: '请选择既往病史',
						// 可以单个或者同时写两个触发验证方式
						trigger: 'blur,change'
					}],
					sex: [{
						required: true,
						message: '请选择性别',
						// 可以单个或者同时写两个触发验证方式
						trigger: 'blur,change'
					}],
					sfid: [{
							required: true,
							message: '请输入身份证',
							// 可以单个或者同时写两个触发验证方式
							trigger: 'blur,change'
						},
						{
							len: 18,
							message: '请正确输入18位身份证',
							// 可以单个或者同时写两个触发验证方式
							trigger: 'blur,change'
						}
					],
					phone: [{
							required: true,
							message: '请输入就诊人手机号',
							// 可以单个或者同时写两个触发验证方式
							trigger: 'blur,change'
						},
						{
							len: 11,
							message: '请正确输入11位手机号',
							// 可以单个或者同时写两个触发验证方式
							trigger: 'blur,change'
						}
					]
				},

			}
		},
		watch: {
			value(newValue, oldValue) {
				//console.log('v-model', newValue);
			}
		},
		computed: {
			...mapState(['token'], ['userInfo']),
			...mapGetters(['hasLogin', 'getUserInfo']),
			getIcon() {
				return path => {
					return 'https://cdn.uviewui.com/uview/example/' + path + '.png';
				}
			}
		},

		onLoad: function(option) {
			this.userInfo.PatientId = option.PatientId
			this.getdetail()
		},
		onShow() {
			if (this.hasLogin) {
				this.GetKinder()
				this.GetListzd()
			} else {
				uni.showModal({
					title: '提示',
					content: '登录后才可以操作',
					confirmText: '去登录',
					showCancel: false,
					success: function(res) {
						uni.reLaunch({
							url: '/pages/auth/login'
						})
					}
				});
				return;
			}
		},
		onReady() {
			this.$refs.form1.setRules(this.rules);
		},
		methods: {

			GetKinder() {
				this.$request("TJChildBase", "GetKinder").then(res => {
					if (res.code == 1) {
						// console.log("获取幼儿园",res)
						let item = res.data
						this.select2.list = []
						// console.log("获取幼儿园",item)
						for (let i = 0; i < item.length; i++) {
							let obj = {
								value: item[i].TJCode,
								label: item[i].TJName
							}
							this.select2.list.push(obj)
						}
						// console.log("this.select2.list",this.select2.list)
					}
				})
			},
			async getdetail() {
				//执行完刷新
				uni.showLoading({
					title: '正在获取个人信息',
					mask: true
				});
				this.$request("TJChildBase", "detail", {
					ChildId: this.userInfo.PatientId
				}).then(res => {
					if (res.code == 1) {
						this.adderssinfor.Province = res.data.TJProvince == null ? "" : res.data.TJProvince,
							this.adderssinfor.City = res.data.TJCity == null ? "" : res.data.TJCity,
							this.adderssinfor.County = res.data.TJCounty == null ? "" : res.data.TJCounty,
							this.userInfo.name = res.data.TJName == null ? "" : res.data.TJName,
							this.userInfo.sfid = res.data.TJIDCardNo == null ? "" : res.data.TJIDCardNo,
							this.userInfo.phone = res.data.TJPhoneNo == null ? "" : res.data.TJPhoneNo,
							this.userInfo.jzsfid = res.data.TJPIDCard == null ? "" : res.data.TJPIDCard,
							this.userInfo.jzname = res.data.TJPName == null ? "" : res.data.TJPName,
							this.userInfo.yeyuan = res.data.TJKinderName == null ? "" : res.data.TJKinderName,
							this.userInfo.TJGradeName = res.data.TJGradeName == null ? "" : res.data.TJGradeName
						this.userInfo.TJClassName = res.data.TJClassName == null ? "" : res.data.TJClassName,
							this.userInfo.TJAddress = res.data.TJAddress == null ? "" : res.data.TJAddress,
							this.userInfo.TJOtherIll = res.data.TJOtherIll == null ? "" : res.data.TJOtherIll,
							this.userInfo.TJIllness = res.data.TJIllness == null ? "" : res.data.TJIllness,
							this.userInfo.TJAllergies = res.data.TJAllergies == null ? "" : res.data.TJAllergies,
							this.userInfo.TJChildId=res.data.TJChildId == null ? "" : res.data.TJChildId
						let obj = [
							res.data.TJProvince == null ? "" : res.data.TJProvince,
							res.data.TJCity == null ? "" : res.data.TJCity,
							res.data.TJCounty == null ? "" : res.data.TJCounty,
						]
						this.adderssinfor.array = obj


					} else {
						console.log("未获取到数据")

					}
				})
				uni.hideLoading();
			},
			GetListzd() {
				this.$request("dictionary", "list", {
					code: 'jwbs',
				}).then(res => {
					if (res.code == 1) {
						console.log("获取既往病史字典", res)
						let item = res.data
						this.select4.list = []
						// console.log("获取幼儿园",item)
						for (let i = 0; i < item.length; i++) {
							let obj = {
								value: item[i].FullName,
								label: item[i].FullName
							}
							this.select4.list.push(obj)
						}
						// console.log("this.select2.list",this.select2.list)
					}
				})
			},
			timePickerChange(e) {
				console.log('picker发送选择改变，携带值为', e)
				let obj = e.year + "-" + e.month + "-" + e.day
				// [
				// 	e.year,
				// 	e.month,
				// 	e.day
				// ]
				this.timeinfor.time = obj
			},
			bindPickerChange(e) {
				console.log('picker发送选择改变，携带值为', e)
				let obj = [
					e.province.name,
					e.city.name,
					e.area.name
				]
				this.adderssinfor.array = obj
				this.adderssinfor.Province = e.province.name
				this.adderssinfor.City = e.city.name
				this.adderssinfor.County = e.area.name
				console.log('picker发送选', this.adderssinfor)
			},
			onSelect1(arr) {
				let current = arr[0];
				console.log("current", current)
				let label = current.label;
				this.select1.current = current;
				this.userInfo.sex = label;
				console.log("this.userInfo.sex", this.userInfo.sex)
			},
			onSelect2(arr) {
				console.log("arr", arr)
				// let current = arr[0];
				// console.log("current", current)
				// let label = current.label;
				this.select2.current = arr[0];
				this.userInfo.yeyuan = arr[0].label;
				this.userInfo.yeyuansy = arr[0].value
				// console.log("this.userInfo.sex", this.userInfo.sex)
			},
			onSelect3(arr) {
				this.select3.current = arr[0];
				this.userInfo.TJGradeName = arr[0].label;
				this.userInfo.TJGradeCode = arr[0].value
				// console.log("this.userInfo.sex", this.userInfo.sex)
			},
			onSelect4(arr) {
				this.select3.current = arr[0];
				this.userInfo.TJIllness = arr[0].label
				// console.log("this.userInfo.sex", this.userInfo.sex)
			},
			submit() {
				this.$refs.form1.validate(valid => {
					if (valid) {
						this.addvisit()
						console.log('验证通过');
					} else {
						console.log('验证失败');
					}
				});
			},
			cancel() {
				this.show = false
			},
			confirm(e) {
				this.sexs = e.value[0]
				this.userInfo.sex = e.value[0]
				this.show = false
			},
			addvisit() {
				console.log(this.userInfo)
				uni.showLoading({
					title: '提交中'
				})
				let user = this.userInfo
				this.$request('TJChildBase', 'submit', {
					TJChildId: user.TJChildId,
					TJName: user.name,
					TJIDCardNo: user.sfid,
					TJPhoneNo: user.phone,
					TJPName: user.jzname,
					TJPIDCard: user.jzsfid,
					TJKinderCode: user.yeyuansy,
					TJKinderName: user.yeyuan,
					TJGradeCode: user.TJGradeCode,
					TJGradeName: user.TJGradeName,
					TJClassName: user.TJClassName,
					TJProvince: this.adderssinfor.Province,
					TJCity: this.adderssinfor.City,
					TJCounty: this.adderssinfor.County,
					TJAddress: user.TJAddress,
					TJIllness: user.TJIllness,
					TJOtherIll: user.TJOtherIll,
					TJAllergies: user.TJAllergies
				}).then(res => {
					console.log(res)
					if (res.code == 1) {
						let doctorinfor = false
						uni.hideLoading();
						uni.redirectTo({
							url: `/pages/block/Visit/Visitcopyadd?doctorinfor=${doctorinfor}`,
							fail(error) {
								console.log(error)
							}
						})
					} else {
						uni.hideLoading();
						uni.showToast({
							title: res.msg,
							icon: 'none',
							duration: 2000
						});
					}
				})
			},
			showToast(params) {
				this.$refs.uToast.show({
					...params,
					complete() {
						params.url && uni.navigateTo({
							url: params.url
						})
					}
				})
			},
			codeChange(text) {
				this.tips = text;
			},
			getCode() {
				console.log(this.userInfo.phone)
				if (this.userInfo.phone == "") {
					this.showToast(this.list)
					return
				}
				if (this.$refs.uCode.canGetCode) {
					// 模拟向后端请求验证码
					uni.showLoading({
						title: '正在获取验证码'
					})
					setTimeout(() => {
						uni.hideLoading();
						// 这里此提示会被this.start()方法中的提示覆盖
						uni.$u.toast('验证码已发送');
						// 通知验证码组件内部开始倒计时
						this.$refs.uCode.start();
					}, 2000);
				} else {
					uni.$u.toast('倒计时结束后再发送');
				}
			},
			change(e) {
				console.log('change', e);
			}
		}
	}
</script>

<style>
	@import './addVisit.css'
</style>